package com.qfedu.user.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qfedu.user.po.Client;
import com.qfedu.user.po.User;
import com.qfedu.user.dao.userDAO;
import com.qfedu.user.util.DBUtil;

public class userDaoimpl implements userDAO {
	private DBUtil DBUtil = new DBUtil();
	private ResultSet rs = null;
	private int intResult = -1;
	private boolean boolResult = false;
	private Client client = null;
	

	@Override
	public Boolean dologin(Object[] objects) {
		String sql = "select * from user_emp where USER_NAME = ? and password = ?";
		ResultSet rs = DBUtil.executeQuery(sql, objects);
		try {
			boolResult = rs.next();
		} catch (SQLException e) {
			e.printStackTrace();
			boolResult = false;
		}
		return boolResult;
	}

	@Override
	public Boolean doVipregister(Object[] objects) {
		// TODO Auto-generated method stub
		String sql = "insert into vipclient(CLIENT_NAME,PHONE_NUM,IDCARD,BIRTHDAY) values(?,?,?,?)";
		intResult = DBUtil.executeUpdate(sql, objects);
		if (intResult != 0) {
			return true;
		} else {
			return false;
		}

	}

	@Override
	public List<Client> doVipSelect(Object[] objects) {
		// TODO Auto-generated method stub
		List<Client> list = new ArrayList<Client>();
		String sql = "select * from vipclient where phone_num=? and idcard=?";
		rs = DBUtil.executeQuery(sql, objects);
		try {
			while (rs.next()) {
				client = new Client(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getString(5),
						rs.getString(6));
				list.add(client);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
			list = null;
		}
		return list;
	}

	@Override
	public boolean doVipDelete(Object[] objects) {
		String sql = "delete from vipclient where idcard=?";
		intResult = DBUtil.executeUpdate(sql, objects);
		if (intResult != 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<Client> doVipselectid(Object[] objects) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Client> doselectOneById(Object[] objects) {
		// TODO Auto-generated method stub
		List<Client> list = new ArrayList<Client>();
		String sql = "select * from vipclient where idcard=?";
		rs = DBUtil.executeQuery(sql, objects);
		try {
			while (rs.next()) {
				client = new Client(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getString(5),
						rs.getString(6));
				list.add(client);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
			list = null;
		}
		return list;
	}

	@Override
	public Boolean doVipUpdateOne(Object[] objects) {
		String sql = "update vipclient set client_name='" + objects[0] + "' ,phone_num='" + objects[1]
				+ "',integration='" + objects[2] + "' where idcard='" + objects[3] + "'";
		System.out.println(sql);
		intResult = DBUtil.executeUpdate(sql, null);
		if (intResult != 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<User> selectOneById1(Object[] objects) {
		
		User user = new User();
		String sql = "select * from user_emp where (USER_ID=" + objects[0] + " or " + objects[0] + " is null) and (name='"
				+ objects[1] + "' or '" + objects[1] + "' = '')";
		System.out.println(sql);
		List<User> list =new ArrayList<>();
		rs = DBUtil.executeQuery(sql, objects);
		try {
			while (rs.next()) {
				user = new User(rs.getInt(1), rs.getString(5), rs.getString(3), rs.getString(4), rs.getString(2),
						rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10));
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			list = null;
		}
		return list;
	}

	@Override
	public boolean doUserRegister(Object[] objects) {
		String sql = "insert into user_emp(USER_NAME,password,POSITION,NAME,SEX,IDCARD,AGE) values(?,?,?,?,?,?,?)";
		intResult = DBUtil.executeUpdate(sql, objects);
		if(intResult!=-1) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public List<User> selectAll() {
		User user= new User();
		String sql="select * from user_emp";
		rs= DBUtil.executeQuery(sql, null);
		List<User> list = new ArrayList<>();
		
		try {
			while(rs.next()) {
			user=new User(rs.getInt(1), rs.getString(5), rs.getString(3), rs.getString(4), rs.getString(2),
						rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10));
			    list.add(user);
			}
			for (User user2 : list) {
				System.out.println(user2);
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
			list =null;
		}
		return list;
	}

	@Override
	public User selectOneById3(Object[] objects) {
		User user = new User();
		String sql = "select * from user_emp where USER_ID = ?";
		System.out.println(sql);
		//List<User> list =new ArrayList<>();
		rs = DBUtil.executeQuery(sql, objects);
		
		if(rs!=null) {
            	try {
					rs.next();
					user = new User(rs.getInt(1), rs.getString(5), rs.getString(3), rs.getString(4), rs.getString(2),
							rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10));
				} catch (SQLException e) {
					
					e.printStackTrace();
					user=null;
				}	
		}
				
				
			return user;
	}
}
